package com.hfzy.ihk.web.callhistoryold.rmi.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.facade.callhistoryold.request.CallHistoryZy4ListRequest;
import com.hfzy.ihk.facade.callhistoryold.request.ClickUserRequest;
import com.hfzy.ihk.facade.callhistoryold.service.CallHistoryRmiService;
import com.hfzy.ihk.web.callhistoryold.biz.CallHistoryOldBiz;
import com.hfzy.ihk.web.callhistoryold.biz.CallhistoryPlayRecordBiz;
import com.hfzy.ihk.web.callhistoryold.entity.CallHistoryOld;
import com.hfzy.ihk.web.callhistoryold.vo.CallHistoryOldVo;
import com.hfzy.ihk.web.callhistoryold.vo.CallHistoryZyReportListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("callHistoryRmiService")
public class CallHistoryRmiServiceImpl implements CallHistoryRmiService {

    @Autowired
    CallHistoryOldBiz callHistoryOldBiz;

    @Autowired
    CallhistoryPlayRecordBiz callhistoryPlayRecordBiz;


    @Override
    public String saveCallHistoryReturnSessionId(Map map) {
        log.info("调用接口:saveCallHistoryReturnSessionId,map:" + JSONObject.toJSONString(map));
        long start = System.currentTimeMillis();
        String rsStr = callHistoryOldBiz.saveCallHistoryReturnSessionId(map);
        log.info("调用接口完成:saveCallHistoryReturnSessionId,耗时:" + (System.currentTimeMillis() - start));
        return rsStr;
    }

    @Override
    public String getCallhistory(Map map) {
        log.info("调用接口:getCallhistory");
        long start = System.currentTimeMillis();
        String rsStr = callHistoryOldBiz.getCallhistory(map);
        log.info("调用接口完成:getCallhistory,耗时:" + (System.currentTimeMillis() - start));
        return rsStr;
    }

    @Override
    public boolean updateCloudCallBySessionId(Map map) {
        log.info("调用接口:updateCloudCallBySessionId,map:" + JSONObject.toJSONString(map));
        long start = System.currentTimeMillis();
        boolean rs = callHistoryOldBiz.updateCloudCallBySessionId(map);
        log.info("调用接口完成:updateCloudCallBySessionId,耗时:" + (System.currentTimeMillis() - start));
        return rs;
    }

    @Override
    public boolean updateCloudCalls(List<Map> mapList) {
        log.info("调用接口:updateCloudCalls,mapList:" + JSONObject.toJSONString(mapList));
        long start = System.currentTimeMillis();
        boolean rs = callHistoryOldBiz.updateCloudCalls(mapList);
        log.info("调用接口完成:updateCloudCalls,耗时:" + (System.currentTimeMillis() - start));
        return rs;
    }

    @Override
    public String queryCallHistory4list(CallHistoryZy4ListRequest request, Integer zyUserId) {
        log.info("调用接口:queryCallHistory4list");
        long start = System.currentTimeMillis();
        Map<String, String> map = new HashMap<>();
        try {
            List<CallHistoryZyReportListVo> list = null;
            if (StringTools.isEmpty(request.getPageSize()) ||
                    StringTools.isEmpty(request.getPage())) {
                map.put("code", "-1");
                map.put("desc", "存在参数为空");
                return JSON.toJSONString(map);
            }
            Map queryMap = callHistoryOldBiz.queryCallHistory4list(request, zyUserId);
            if(queryMap.get("data") != null){
                list = (List<CallHistoryZyReportListVo>)(queryMap.get("data"));
            }
            map.put("code", "1");
            map.put("jsonArray", list != null ? JSON.toJSONString(list) : null);
            map.put("total",queryMap.get("total") != null?queryMap.get("total").toString():"0");
            map.put("desc", "成功");
            log.info("调用接口完成:queryCallHistory4list,耗时:" + (System.currentTimeMillis() - start));
            return JSON.toJSONString(map);
        } catch (Exception e) {
            log.error("报错:queryCallHistory4list",e);
            e.printStackTrace();
            map.put("code", "-2");
            map.put("desc", "查询异常");
            return JSON.toJSONString(map);
        }
    }


    @Override
    public String getCallRecords(Map paramMap) {
        log.info("开始:调用定制机接口:getCallRecords,paramMap:" + paramMap);
        long start = System.currentTimeMillis();
        String rs = callHistoryOldBiz.getCallRecords(paramMap);
        log.info("结束:调用定制机接口:getCallRecords,耗时:" + (System.currentTimeMillis() - start));
        return rs;
    }

    @Override
    public void addCallhistoryPlayRecord(String json) {
        log.info("开始:调用接口:addCallhistoryPlayRecord,json:" + json);
        long start = System.currentTimeMillis();
        callhistoryPlayRecordBiz.addCallhistoryPlayRecord(json);
        log.info("结束:调用接口:addCallhistoryPlayRecord,耗时:" + (System.currentTimeMillis() - start));
    }

    @Override
    public void updateClickUser(ClickUserRequest clickUserRequest){
        log.info("开始:调用接口:updateClickUser,参数:" + JSONObject.toJSONString(clickUserRequest));
        long start = System.currentTimeMillis();

        if(clickUserRequest.getClickTime() == null || clickUserRequest.getClickUserId() == null
                || clickUserRequest.getClickUserId() == 0 || StringTools.isEmpty(clickUserRequest.getClickUserName())
                || StringTools.isEmpty(clickUserRequest.getSessionId())){
            log.info("调用接口:updateClickUser，缺少必要参数");
            return;
        }
        callHistoryOldBiz.updateClickUser(clickUserRequest);

        log.info("结束:调用接口:updateClickUser,耗时:" + (System.currentTimeMillis() - start));
    }

    @Override
    public String geCallHistoryCrms(String customerIds, String sessionIds, Integer clickUserId) {
        Map rsMap = new HashMap<>();
        try {
            log.info("===>geCallHistoryCrms:customerIds:" + customerIds + ",sessionIds:" + sessionIds );
            long start = System.currentTimeMillis();

            List<CallHistoryOldVo> list = callHistoryOldBiz.geCallHistoryCrms(customerIds,sessionIds,clickUserId);
            rsMap.put("data", list);
            rsMap.put("result", "10000");
            rsMap.put("msg", "成功");

            long end = System.currentTimeMillis();
            log.info("结束===>geCallHistoryCrms:customerIds:" + customerIds + ",sessionIds:" + sessionIds
                    + ",耗时:" + (end - start));
        }catch (Exception e){
            log.info("报错===>geCallHistoryCrms:customerIds:" + customerIds + ",sessionIds:" + sessionIds );
            e.printStackTrace();
//            rsMap.put("result", AppErrorMsgEnum.QUERY_ERROR.toStr());
//            rsMap.put("msg", AppErrorMsgEnum.QUERY_ERROR.getDesc());
        }finally {
            return JSONObject.toJSONString(rsMap, SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullBooleanAsFalse);
        }
    }
}
